//
//  UILabel+ZZZCategory.m
//  zjhealth
//
//  Created by 张润东 on 2017/11/30.
//  Copyright © 2017年 amu. All rights reserved.
//

#import "UILabel+ZZZCategory.h"

@implementation UILabel (ZZZCategory)

//倒计时
-(void)timeDisCount:(NSInteger)seconds{
    
    __block NSInteger timeout =seconds-1;
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    //设置间隔时间
    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0*NSEC_PER_SEC, 0);
    //timer回调
    dispatch_source_set_event_handler(_timer, ^{
        if (timeout<=0) {
            //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                self.text=@"00:00:00";
                self.font = [UIFont systemFontOfSize:16];
                self.textColor=[UIColor colorWithRed:204/255 green:204/255 blue:204/255 alpha:1];
                
            });
        }else
        {
            
            NSInteger hours = timeout / 3600;
            NSInteger minutes = (timeout - (timeout / 3600) * 3600) / 60;
            NSInteger seconds = timeout % 60;
            
            NSString *hoursStr = @"00";
            NSString *minutesStr = @"00";
            NSString *secondsStr = @"00";
            
            if (hours < 10) {
                hoursStr = [NSString stringWithFormat:@"0%ld",(long)hours];
            }
            else {
                hoursStr = [NSString stringWithFormat:@"%ld",(long)hours];
            }
            
            if (minutes < 10) {
                minutesStr = [NSString stringWithFormat:@"0%ld",(long)minutes];
            }
            else {
                minutesStr = [NSString stringWithFormat:@"%ld",(long)minutes];
            }
            
            if (seconds < 10) {
                secondsStr = [NSString stringWithFormat:@"0%ld",(long)seconds];
            }
            else {
                secondsStr = [NSString stringWithFormat:@"%ld",(long)seconds];
            }
            
            
            NSString*strtime = [NSString stringWithFormat:@"%@:%@:%@",hoursStr,minutesStr,secondsStr];
            //更新ui
            dispatch_async(dispatch_get_main_queue(), ^{
                self.text=strtime;
            });
            
            timeout--;
            
        }
    });
    dispatch_resume(_timer);//开始计时
}


- (void)changeLineSpace:(float)space {
    
    NSString *labelText = self.text;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:space];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    self.attributedText = attributedString;
    [self sizeToFit];
}

@end
